package com.zahb.qadx.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.google.gson.Gson;
import com.zahb.qadx.MainActivity;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.model.ArchivesBean;
import com.zahb.qadx.model.WorkExRequest;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.adapter.WorkExperienceAdapter;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;


public class WorkExperienceActivity extends BaseActivity {

    List<WorkExRequest> list = new ArrayList<>();
    WorkExperienceAdapter adapter;
    @BindView(R.id.llNoEx)
    LinearLayout llNoEx;
    @BindView(R.id.tvAdd)
    TextView tvAdd;
    @BindView(R.id.recyc)
    RecyclerView recyc;
    @BindView(R.id.tvSave)
    TextView tvSave;


    int type=0;

    boolean first = false;

    @Override
    protected int getLayout() {
        return R.layout.activity_work_experience;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);

        type = getIntent().getIntExtra("type", 0);

        if (type == 1) {//不可编辑
            adapter = new WorkExperienceAdapter(R.layout.item_work_ex_content, list);
        } else {
            adapter = new WorkExperienceAdapter(R.layout.item_work_ex, list);
        }

        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int pos) {

                if(view.getId() == R.id.content){
                    if(type == 1){
                        return;
                    }
                    Intent intent = new Intent(WorkExperienceActivity.this, AddWorkExActivity.class);
                    intent.putExtra("type",type);
                    intent.putExtra("data",list.get(pos));
                    intent.putExtra("datatwo",pos);
                    startActivityForResult(intent, 111);
                }else{
                    if(list.size()>pos){
                        list.remove(pos);
                        adapter.notifyItemRemoved(pos);
                        if(list.size()==0){
                            llNoEx.setVisibility(View.VISIBLE);
                        }else{
                            llNoEx.setVisibility(View.GONE);
                        }
                    }
                }


            }
        });

        recyc.setAdapter(adapter);
        recyc.setLayoutManager(new LinearLayoutManager(this));
        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WorkExperienceActivity.this, AddWorkExActivity.class);
                if(list.size()==0){
                    intent.putExtra("datatwo",-1);
                    intent.putExtra("data",workExRequest);
                }else{
                    intent.putExtra("datatwo",-2);
                }
                startActivityForResult(intent, 111);

               // first=false;
            }
        });

        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(list.size()==0){
                    showBindToast("请添加工作经历");
                    return;
                }
                Map<String, Object> params = new HashMap<>();
                params.put("userWorkExperienceList", list);

                Gson mGson = new Gson();
                String json = mGson.toJson(params);

                RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
                addDisposable(RetrofitService.getNetService()
                        .archives_experience(body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(response -> {
                            if (response.getStatusCode() == 200) {
                                Intent intent = new Intent(WorkExperienceActivity.this, MainActivity.class);
                                intent.putExtra("type",1);
                                startActivity(intent);
                                 finish();
                            } else {
                                showBindToast(response.getErrorInfo());
                            }
                        }, throwable -> {
                            throwable.printStackTrace();
                            Tips.RequestError(getActivity());
                        }));
            }
        });

        if(type == 1){
            tvSave.setVisibility(View.INVISIBLE);
            tvAdd.setVisibility(View.GONE);
        }else{
            first = true;
        }

        if(MyArchivesActivity.archive!=null){

            if(type==0){
                ArchivesBean.InitializationWorkExperienceEntity entity = MyArchivesActivity.archive.getInitializationWorkExperience();
                if(entity!=null){
                    workExRequest = new WorkExRequest();
                    workExRequest.company = entity.getCompany();
                    workExRequest.job = entity.getJob();
                    if(entity.getStartDate().length()>=7)
                        workExRequest.startDate = entity.getStartDate().substring(0,7);
                    workExRequest.endDateType=0;
                    workExRequest.endDate="";

                }
            }else{
                if(MyArchivesActivity.archive.getExperience()!=null&& MyArchivesActivity.archive.getExperience().getUserWorkExperienceList()!=null){
                    list.addAll(MyArchivesActivity.archive.getExperience().getUserWorkExperienceList());
                    adapter.notifyDataSetChanged();
                }
            }
            if(list.size()==0){
               // llNoEx.setVisibility(View.VISIBLE);
            }else{
                llNoEx.setVisibility(View.GONE);
            }
        }



    }

    WorkExRequest workExRequest = new WorkExRequest();

    @Override
    protected int getTitleStringRes() {
        return R.string.work_experience;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 110) {
                if (data != null) {
                    WorkExRequest workExRequest = data.getParcelableExtra("data");

                    list.add(workExRequest);
                    adapter.notifyDataSetChanged();

                    llNoEx.setVisibility(View.GONE);
                }
            }
            if (requestCode == 111) {
                if (data != null) {
                    WorkExRequest workExRequest = data.getParcelableExtra("data");
                    int p = data.getIntExtra("datatwo",-1);
                    if(p>=0){

                        WorkExRequest old =  list.get(p);
                        old.endDate = workExRequest.endDate;
                        old.endDateType = workExRequest.endDateType;
                        old.startDate = workExRequest.startDate;
                        old.job = workExRequest.job;
                        old.company = workExRequest.company;

                        adapter.notifyItemChanged(p);
                    }else{

                        list.add(workExRequest);
                        adapter.notifyDataSetChanged();
                    }
                    llNoEx.setVisibility(View.GONE);

                }
            }
        }
    }


}
